﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ServerNodeGui
{
	public partial class ClientFileBrowserDialog : Form
	{
		private List<FileChange> fileChanges;
		public List<FileChange> Changes
		{
			get
			{
				return this.fileChanges;
			}
		}

		public ClientFileBrowserDialog(FileNode root)
		{
			InitializeComponent();
			this.fileChanges = new List<FileChange>();

			if (root == null)
				throw new ArgumentNullException("root");
			TreeNode tn= new TreeNode("Root");
			rec_buildTree(tn,root);
			treeDirectory.Nodes.Add(tn);
		}

		private void rec_buildTree(TreeNode parent, FileNode root)
		{
			TreeNode newNode = new TreeNode();
			newNode.Text = root.Filename;
			if (root.IsDirectory)
				newNode.Text += System.IO.Path.DirectorySeparatorChar;
			newNode.Tag = root;
			foreach(FileNode child in root.Children)
			{
				rec_buildTree(newNode, child);
			}
		}

		private void btnBrowse_Click(object sender, EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog();
			if (ofd.ShowDialog() == DialogResult.OK)
			{
				this.txtUpload.Text = ofd.FileName;
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtUpload.Text))
			{
				MessageBox.Show("The upload file name is empty");
			}

			if (System.IO.File.Exists(txtUpload.Text) == false)
			{
				MessageBox.Show("The upload file does not exist");
			}

			TreeNode tn = treeDirectory.SelectedNode;
			if (tn == null)
			{
				MessageBox.Show("Pleaes select a directory to place this file");
			}

			if (((FileNode)tn.Tag).IsDirectory == false)
			{
				MessageBox.Show("Pleaes select a directory, not a file.");
			}

			FileChange fc = new FileChange();
			fc.SourcePath = txtUpload.Text;
			fc.DestinationPath = tn.FullPath;
			fc.FileChangeType = FileChangeType.Add;

			this.fileChanges.Add(fc);

			TreeNode newNode = new TreeNode(fc.ToString());
			tn.Nodes.Add(newNode);
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			TreeNode tn = treeDirectory.SelectedNode;
			if (tn == null)
			{
				MessageBox.Show("Pleaes select a file to remove");
			}

			FileChange fc = new FileChange();
			fc.DestinationPath = tn.FullPath;
			fc.FileChangeType = FileChangeType.Delete;

			this.fileChanges.Add(fc);

			tn.Remove();
		}

		private void btnRename_Click(object sender, EventArgs e)
		{

			TreeNode tn = treeDirectory.SelectedNode;
			if (tn == null)
			{
				MessageBox.Show("Pleaes select a file to rename");
			}
			SaveFileDialog ofd = new SaveFileDialog();
			if (ofd.ShowDialog() == DialogResult.OK)
			{
				tn.Text = System.IO.Path.GetFileName(ofd.FileName);
			}
		}
	}

	public class FileChange
	{
		/// <summary>
		/// The path for defining the source of uploads
		/// </summary>
		public String SourcePath;
		/// <summary>
		/// The path for defining the destination file name.
		/// </summary>
		public String DestinationPath;
		/// <summary>
		/// The type of file system change.
		/// </summary>
		public FileChangeType FileChangeType;

		public override string ToString()
		{
			return System.IO.Path.GetFileName(DestinationPath) + " " + FileChangeType.ToString();
		}
	}

	public enum FileChangeType
	{
		Add,
		Delete,
		Rename,
	}

	public class FileNode
	{
		public String Filename;
		public String Md5;
		public Boolean IsDirectory;
		public List<FileNode> Children;

		public FileNode()
			: this(string.Empty)
		{
		}

		public FileNode(String fn)
		{
			this.Children = new List<FileNode>();
			this.Md5 = String.Empty;
			this.Filename = fn;
		}
	}
}
